package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupDto;
import com.itheima.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//检出组相关接口
@Slf4j
@RequestMapping("/checkgroup")
@RestController
public class CheckgroupController {
    @Autowired
    private CheckgroupService checkgroupService;

    //检查组分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPage) {

        return checkgroupService.findPage(queryPage);

    }

    //检查组新增
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam Integer[] checkitemIds) {

        log.info("新增检查组 {} {}", checkGroup, checkitemIds);
        checkgroupService.add(checkGroup, checkitemIds);

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //根据id查询检查组
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        log.info("根据id查询检查组 {}", id);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupService.findById(id));
    }

    //编辑检查组
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,
                       @RequestParam Integer[] checkitemIds) {
        log.info("编辑检查组 {} {}", checkGroup, checkitemIds);

        checkgroupService.edit(checkGroup, checkitemIds);

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
    //查询所有检查组
    @GetMapping("/findAll")
    public Result findAll() {

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupService.findAll());
    }

    //根据id删除检查组
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        log.info("删除检查项 {}" , id);

        //删除检查组方法
        checkgroupService.delete(id);

        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS, null);
    }

    //根据套餐id查询关联的检查组id
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId) {
        log.info("根据检查组id查询关联的检查项id {}", setmealId);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupService.findCheckGroupIdsBySetmealId(setmealId));
    }
}
